package cn.gzmli.dtt.common.controll;


import cn.gzmli.dtt.common.annotation.Comment;
import cn.gzmli.dtt.common.annotation.Privilege;
import cn.gzmli.dtt.common.entity.PageInfo;
import cn.gzmli.dtt.common.entity.Result;
import cn.gzmli.dtt.common.entity.Role;
import cn.gzmli.dtt.common.service.RoleService;
import cn.gzmli.dtt.common.utils.BaseController;
import cn.gzmli.dtt.common.utils.Constant;
import cn.gzmli.dtt.sbzyc.validation.Delete;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Descript 角色相关接口
 * @Author xsx
 * @Date 2017/12/25
 */
@Controller
@RequestMapping("/role")
public class RoleController implements BaseController<Role> {
    @Autowired
    private RoleService roleService;

    @Override
    @RequestMapping("/list")
    @ResponseBody
    @Comment("获取所有角色信息列表")
    public Result list() throws Exception {
        return new Result(Constant.SUCCESS, "获取角色列表成功", this.roleService.getAll());
    }

    @RequestMapping("/listAllRoles")
    @ResponseBody
    @Comment("获取所有角色信息列表（只包含id和name）")
    public Result listAllRole() throws Exception {
        return new Result(Constant.SUCCESS, "获取角色列表成功", this.roleService.queryAllRoles());
    }

    @Override
    @RequestMapping("/listByCondition")
    @ResponseBody
    @Comment("获取角色列表")
    public Result listByCondition(HttpServletRequest request, PageInfo pageInfo) throws Exception {
        List<Map<String, Object>> data = this.roleService.getByCondition(request, pageInfo);
        return new Result(Constant.SUCCESS, "获取角色列表成功", data, pageInfo);
    }

    @Override
    @RequestMapping("/get")
    @ResponseBody
    @Comment("获取角色信息")
    public Result get(Integer id) throws Exception {
        return new Result(Constant.SUCCESS, "获取角色信息成功", this.roleService.get(id));
    }

    @Override
    @RequestMapping("/getByCondition")
    @ResponseBody
    @Comment("获取角色信息")
    public Result getByCondition(HttpServletRequest request) throws Exception {
        Map<String, String> condition = new HashMap<>();
        return new Result(Constant.SUCCESS, "获取角色信息成功", this.roleService.getByCondition(condition));
    }

    @Override
    @RequestMapping("/save")
    @ResponseBody
    @Comment("新增角色信息")
    @Privilege("/role/save")
    public Result save(Role role) throws Exception {
        return new Result(Constant.SUCCESS, "新增角色信息成功", this.roleService.save(role));
    }

    @Override
    @RequestMapping("/update")
    @ResponseBody
    @Comment("修改角色信息")
    @Privilege("/role/update")
    public Result update(Role role) throws Exception {
        this.roleService.update(role);
        return new Result(Constant.SUCCESS, "修改角色信息成功");
    }

    @Override
    @RequestMapping("/delete")
    @ResponseBody
    @Comment("删除角色信息")
    @Privilege("/role/delete")
    public Result delete(@Validated(Delete.class)Role role) throws Exception {
        this.roleService.delete(role);
        return new Result(Constant.SUCCESS, "删除角色信息成功");
    }

    @RequestMapping("/listTree")
    @ResponseBody
    @Ignore
    public Result listTree(HttpServletRequest request) throws Exception {
        return new Result(1, "获取角色列表成功", this.roleService.getTree(request));
    }

    @RequestMapping("/updateMenu")
    @ResponseBody
    @Comment("更新角色菜单")
    @Privilege("/role/updateMenu")
    public Result updateMenu(Integer id, String menuIDS) throws Exception {
        this.roleService.updateMenus(id, menuIDS);
        return new Result(Constant.SUCCESS, "更新角色菜单成功");
    }
    
    @RequestMapping("/updatePermisson")
    @ResponseBody
    @Comment("更新权限菜单")
    @Privilege("/role/updatePermisson")
    public Result updatePermisson(Integer id, String permissionIDS) throws Exception {
        this.roleService.updatePermissons(id, permissionIDS);
        return new Result(Constant.SUCCESS, "更新权限菜单成功");
    }


	@Override
	public Result lists(HttpServletRequest request, PageInfo pageInfo) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}


}
